<template>
  <div class="customerManage">
    <view-header :title="'学生管理'"></view-header>
    <div class="search_area">
      <el-row>
        <el-form :form="queryForm" label-width="1rem">
          <el-col :span="8">
            <el-form-item label="学号：">
              <el-input type="text" v-model="queryForm.account" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名：">
              <el-input type="text" v-model="queryForm.userName" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="院系：">
              <el-input type="text" v-model="queryForm.department" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="专业：">
              <el-input type="text" v-model="queryForm.specialty" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="班级：">
              <el-input type="text" v-model="queryForm.className" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-form>
        <el-col :span="8">
          <div class="btn_area">
            <el-button class="confirm" @click="btnClick">查询</el-button>
            <el-button class="cancle" @click="reset">重置</el-button>
            <el-button class="confirm" @click="add">新增</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <table-list :fields="fields" :list="list" :pagination="pagination" :total="total" @sizeChange="handleSizeChange" @currentChange="handleCurrentChange" @handle="onHandle"></table-list>
    <el-dialog :title="code == '1' ? '新增学生信息': '编辑学生信息'" :visible.sync="visible" @close="close" width="60%">
      <el-row>
        <el-form :form="studentForm" label-width="1.2rem">
          <el-col :span="11">
            <el-form-item label="学号：" required>
              <el-input v-model="studentForm.account" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="姓名：" required>
              <el-input v-model="studentForm.userName" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="性别：" required>
              <el-radio v-model="studentForm.gender" label="1">男</el-radio>
              <el-radio v-model="studentForm.gender" label="0">女</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="联系方式：" required>
              <el-input v-model="studentForm.phone" placeholder="请输入" maxlength="11"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="家庭住址：" required>
              <el-input v-model="studentForm.address" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="院系：" required>
              <el-input v-model="studentForm.department" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="专业：" required>
              <el-input v-model="studentForm.specialty" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="班级：" required>
              <el-input v-model="studentForm.className" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="用户状态：" required>
              <el-radio v-model="studentForm.status" label="1">启用</el-radio>
              <el-radio v-model="studentForm.status" label="0">禁用</el-radio>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="11">
            <el-form-item label="宿舍楼">
              <el-input readonly v-model="studentForm.cost" placeholder="此项不用输入，由宿管职工配置"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="宿舍号">
              <el-input readonly v-model="studentForm.cost" placeholder="此项不用输入，由宿管职工配置"></el-input>
            </el-form-item>
          </el-col> -->
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancle" @click="visible = false">取 消</el-button>
        <el-button class="confirm" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import tableList from '@/components/table.vue'
import viewHeader from '@/components/viewHeader.vue'
export default {
  name: 'student',
  components: {
    tableList,
    viewHeader
  },
  data() {
    return {
      queryForm: {
        account: "",
        className: "",
        department: "",
        specialty: "",
        userName: ""
      },
      list: [],
      fields: [
        {
          prop: 'rowNum',
          label: '序号',
          align: 'center'
        },
        {
          prop: 'account',
          label: '学号',
          align: 'center'
        },
        {
          prop: 'userName',
          label: '姓名',
          align: 'center'
        },
        {
          prop: 'genderName',
          label: '性别',
          align: 'center'
        },
        {
          prop: 'phone',
          label: '联系方式',
          align: 'center'
        },
        {
          prop: 'address',
          label: '家庭住址',
          align: 'center'
        },
        {
          prop: 'department',
          label: '院系',
          align: 'center'
        },
        {
          prop: 'specialty',
          label: '专业',
          align: 'center'
        },
        {
          prop: 'className',
          label: '班级',
          align: 'center'
        },
        {
          prop: 'statusName',
          label: '状态',
          align: 'center'
        },
        {
          prop: 'oper',
          label: '操作',
          align: 'center',
          btns: [
            {type: 'success',desc:'编辑'}
          ]
        }
      ],
      pagination: {
        currentPage: 1,
        pageSize: 10
      },
      total: 0,
      visible: false,
      code: '', // 1新增，2编辑、查看详情
      studentForm: {},
      user: {}
    }
  },
  methods: {
    confirm(){
      let url = this.code == '1' ? this.$api.user.create : this.$api.user.update
      console.log(this.studentForm);
      if(this.code == '1') {
        this.studentForm.permission = '04'
        this.studentForm.password = '123456'
      }
      this.$http(url,this.reqData(this.studentForm)).then(res => {
        if (res.data.code == 200) {
          let msg = this.code == '1' ? res.data.message + ',' + '用户初始密码为：123456' : res.data.message
          this.$message.success(msg);
          this.visible = false
          this.queryStudentList();
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    close() {
      this.studentForm = {};
    },
    add() {
      this.code = '1';
      this.visible = true;
    },
    reset() {
      this.queryForm = {
        account: "",
        className: "",
        department: "",
        specialty: "",
        userName: ""
      };
      this.queryStudentList();
    },
    onHandle(data) {
      let {row,type} = data;
      this.code = '2'
      this.studentForm = row
      this.visible = true;
    },
    btnClick() {
      console.log(111);
      this.queryStudentList();
    },
    queryStudentList() {
      this.queryForm.queryPermission = '04'
      this.$http(this.$api.user.list,this.reqData(this.queryForm,this.pagination.currentPage,this.pagination.pageSize)).then(res => {
        if (res.data.code === 200) {
          let data = res.data.data;
          this.list = data.list;
          this.list.forEach((item,index) => {
            item.rowNum = (this.pagination.currentPage - 1) * this.pagination.pageSize + index + 1
            item.genderName = item.gender == '0' ? '女' : '男'
            item.statusName = item.status == '1' ? '启用' : '禁用'
          })
          this.total = this.list.length;
        } else {
          this.$message.error(res.data.message)
        }
      })
    },
    handleSizeChange(val) {
      console.log(val);
      this.pagination.pageSize = val;
      this.queryStudentList();
    },
    handleCurrentChange(val) {
      console.log(val);
      this.pagination.currentPage = val;
      this.queryStudentList();
    }
  },
  mounted() {
    this.user = JSON.parse(sessionStorage.getItem('user'));
    this.queryStudentList();
  }
}
</script>
<style lang="less" scoped>
.customerManage {
  background: #fff;
  overflow: hidden;
  padding-bottom: 0.15rem;
}
</style>